

package yf.mall.service.impl;

import org.springframework.transaction.annotation.Transactional;
import yf.mall.entity.LevelEntity;
import yf.mall.entity.UserEntity;
import yf.mall.service.LevelService;
import yf.mall.service.UserService;
import yf.mall.vo.up.LevelRecordUp;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.excel.context.AnalysisContext;

import java.io.InputStream;

import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yf.mall.vo.down.LevelRecordDown;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;

import yf.mall.vo.down.LevelRecordDown;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

import yf.mall.dao.LevelRecordDao;
import yf.mall.entity.LevelRecordEntity;
import yf.mall.vo.LevelRecordVo;
import yf.mall.service.LevelRecordService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 *
 *
 * @date Tue Apr 25 09:50:33 CST 2023
 */
@Service
public class LevelRecordServiceImpl extends BaseServiceImpl<LevelRecordEntity> implements LevelRecordService {

    private LevelRecordDao dao;


    @Autowired
    public void setDao(LevelRecordDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }


    @Override
    public List<LevelRecordVo> findVo(LevelRecordVo vo) {
        return dao.findVo(vo);
    }

    @Override
    public LevelRecordVo getVo(Long id) {
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(LevelRecordVo vo) {
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(LevelRecordVo vo) {
        return dao.update(vo.getEntity());
    }


    @Override
    public void downLoad(HttpServletResponse response, List<LevelRecordVo> list) {
        try {
            List<LevelRecordDown> datas = list.stream().map(item -> new LevelRecordDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), LevelRecordDown.class).sheet("模板").doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, LevelRecordUp.class, new UploadDataListener()).sheet().doRead();
    }

    class UploadDataListener extends AnalysisEventListener<LevelRecordUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(LevelRecordUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }


    /**内部方法**/


    /**
     * 外部方法实现
     **/


    @Resource
    private UserService userService;

    @Resource
    private LevelService levelService;

    @Override
    public void addRecord(Long userId, Long nowLvId, Long oldLvId,String orderNo, String remark) {
        UserEntity user = userService.get(userId);
        LevelEntity nowLevel = levelService.get(nowLvId);
        LevelEntity oldLevel = levelService.get(oldLvId);
        LevelRecordEntity record = new LevelRecordEntity(userId, user.getName(), user.getCode(), nowLvId, nowLevel.getName(), oldLevel.getName(),
                oldLvId,orderNo, remark, new Date());
        save(record);
    }

    @Override
    public List<LevelRecordEntity> findByMonth(Date date) {
        return dao.findByMonth(date);
    }
}
